package com.example.plan.common.result;

import cn.hutool.http.HttpStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/*
 * @author hpt
 * Created on 2024/7/30
 */
@ApiModel(value = "返回结果")
public class Result<T> implements IResult {

    /**
     * 返回编码
     */
    @ApiModelProperty(value = "返回编码")
    private int code;
    /**
     * 返回消息
     */
    @ApiModelProperty(value = "返回消息")
    private String message;
    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据")
    private T data;

    public Result() {}

    private Result(int code, String message) {
        this.code = code;
        this.message = message;
        this.data = null;
    }

    private Result(int code, String message, T data) {
        this.message = message;
        this.data = data;
        this.code = code;
    }

    public static <T> Result<T> success() {
        return success(BaseCodeEnum.SUCCESS.getCode(), BaseCodeEnum.SUCCESS.getMessage(), null);
    }

    public static <T> Result<T> success(String message) {
        return success(BaseCodeEnum.SUCCESS.getCode(), message, null);
    }

    public static <T> Result<T> success(int code, String message) {
        return success(code, message, null);
    }

    public static <T> Result<T> success(String message, T data) {
        return success(BaseCodeEnum.SUCCESS.getCode(), message, data);
    }

    public static <T> Result<T> success(T data) {
        return success(BaseCodeEnum.SUCCESS.getCode(), BaseCodeEnum.SUCCESS.getMessage(), data);
    }

    public static <T> Result<T> success(int code, String message, T data) {
        return new Result<>(code, message, data);
    }

    public static <T> Result<T> failed() {
        return failed(BaseCodeEnum.FAILED.getCode(), BaseCodeEnum.FAILED.getMessage(), null);
    }

    public static <T> Result<T> failed(String message) {
        return failed(BaseCodeEnum.FAILED.getCode(), message, null);
    }

    public static <T> Result<T> failed(int code, String message) {
        return failed(code, message, null);
    }

    public static <T> Result<T> failed(String message, T data) {
        return failed(BaseCodeEnum.FAILED.getCode(), message, data);
    }

    public static <T> Result<T> failed(T data) {
        return failed(BaseCodeEnum.FAILED.getCode(), BaseCodeEnum.FAILED.getMessage(), data);
    }

    public static <T> Result<T> failed(int code, String message, T data) {
        return new Result<>(code, message, data);
    }

    @Override
    public String getMessage() {
        return message;
    }

    /**
     * 是否成功
     *
     * @return code=200 返回true; 否则返回false;
     */
    @Override
    public boolean isSuccess() {
        int code = this.getCode();
        return code == BaseCodeEnum.SUCCESS.getCode();
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    @SuppressWarnings("unchecked")
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "Result{" + "message='" + message + '\'' + ", data=" + data + ", code=" + code + '}';
    }

    /**
     * 基础通用枚举
     */
    public enum BaseCodeEnum {
        /**
         * 成功
         */
        SUCCESS(HttpStatus.HTTP_OK, "成功"),
        /**
         * 失败
         */
        FAILED(HttpStatus.HTTP_INTERNAL_ERROR, "失败");

        /**
         * 响应码
         */
        private final Integer code;
        /**
         * 响应信息
         */
        private final String message;

        BaseCodeEnum(Integer code, String message) {
            this.code = code;
            this.message = message;
        }

        public Integer getCode() {
            return code;
        }

        public String getMessage() {
            return message;
        }
    }

}
